package com.changjiang.myproductsale.myproduct.servlet;
import com.changjiang.myproductsale.myproduct.dao.impl.UserImpl;
import com.changjiang.myproductsale.myproduct.entity.User;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Locale;

@WebServlet(name = "ServletLogin", value = "/slogin")
public class ServletLogin extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //处理get请求
        this.doPost(request,response);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //处理post
        //临时测试
        //response.setContentType("text/html;charset=UTF-8");
        //response.getWriter().print("你好 hello world");
        String oper = request.getParameter("oper");
        if(oper.equals("login")){
            //登陆操作
            //处理登陆的请求
            //设置编码格式
            request.setCharacterEncoding("UTF-8");
            //验证码
            String ckNo=request.getParameter("checkno").toLowerCase(); //用户输入的
            String code=request.getSession().getAttribute("code").toString().toLowerCase();//session中
            if(!ckNo.equals(code)){
                request.setAttribute("msg","验证码输入错误!");
                request.getRequestDispatcher("/login.jsp").forward(request,response);
            }


            //接受提交的数据
            String uname = request.getParameter("username");
            String upwd = request.getParameter("password");

            //完成使用cookie记住账号和密码
            String [] remember = request.getParameterValues("remember");
            if(remember==null){
                //没有勾选 删除cookie
                Cookie cookieName=new Cookie("uname","");
                Cookie cookiePwd=new Cookie("upwd","");

                //设置cookie过期时间
                cookieName.setMaxAge(0);
                cookiePwd.setMaxAge(0);
                //设置??
                cookieName.setPath("/");
                cookiePwd.setPath("/");
                //写入到客户端浏览器
                response.addCookie(cookieName);
                response.addCookie(cookiePwd);

            }else{
                //勾选了
                Cookie cookieName=new Cookie("uname",uname);
                Cookie cookiePwd=new Cookie("upwd",upwd);
                //设置cookie过期时间
                cookieName.setMaxAge(3600*24*7);
                cookiePwd.setMaxAge(3600*24*7);
                //设置cookie存放的位置 跟着网站走
                cookieName.setPath("/");
                cookiePwd.setPath("/");
                //写入到客户端浏览器
                response.addCookie(cookieName);
                response.addCookie(cookiePwd);

            }
            //调用dao模式写好的类进数据库验证
            UserImpl userImpl=new UserImpl();
            User user= userImpl.login(uname,upwd);
            if(user==null){
                //out.println("登陆失败");
                //用转发
                request.setAttribute("msg","用户名或者密码错误!");
                request.getRequestDispatcher("/login.jsp").forward(request,response);
            }else{
                //在session中存储一下登陆的结果  存储这个对象在session中
                HttpSession session =  request.getSession();
                session.setAttribute("user",user);
                //跳转到首页
                response.sendRedirect("/product/index");
            }

        }else if(oper.equals("loginout")){
            //退出登陆
            //①session删除
            request.getSession().removeAttribute("user");
            //②跳转会登录页
            response.sendRedirect("/product/login.jsp");
        }else
            if(oper.equals("register")){
            //注册操作
            request.setCharacterEncoding("UTF-8");
            //接受提交的数据
            String username = request.getParameter("username");
            String userpwd = request.getParameter("userpwd");
            String userrepwd = request.getParameter("userrepwd");
            String userphone = request.getParameter("userphone");
            String checkno = request.getParameter("checkno");
            //验证
            if(!userpwd.equals(userrepwd)){
                request.setAttribute("msg","两次输入的密码不一致!");
                request.getRequestDispatcher("/register.jsp").forward(request,response);
            }
            if(userphone.length()==11){
                request.setAttribute("msg","手机号格式不正确!");
                request.getRequestDispatcher("/register.jsp").forward(request,response);
            }
            //找DAO
            UserImpl userImpl=new UserImpl();
            User user=new User();
            user.setUser_name(username);
            user.setPay_password(userpwd);
            user.setUser_number(userphone);

            if(userImpl.save(user)){
                HttpSession session =  request.getSession();
                session.setAttribute("user",user);
                //跳转到首页
                response.sendRedirect("/product/index");
            }else{
                request.setAttribute("msg","注册失败!");
                request.getRequestDispatcher("/register.jsp").forward(request,response);
            }

        }else{
            response.sendRedirect("404.html");
        }

    }
}
